<?
session_start();
include('conn.php');
date_default_timezone_set('Asia/Kolkata');

$custid = $_REQUEST['custid'];

//*** Start Transaction ***//  
mysql_query("START TRANSACTION");  

$sql1 = "update invoiceconfig set creditnotenumber = creditnotenumber + 1 where CURRENT_TIMESTAMP() between financialyearstart and financialyearend";
$res1= mysql_query($sql1) or die(mysql_error());

$sql2 = "select creditnotenumber from invoiceconfig where CURRENT_TIMESTAMP() between financialyearstart and financialyearend";
$res2= mysql_query($sql2);
$res = mysql_fetch_assoc($res2);
$creditnoteno=0;
$creditnoteno = $res['creditnotenumber'];
		
$sqlupdate = "update transactionmast set creditnotenumber=".$creditnoteno.",creditnotedate=CURRENT_TIMESTAMP() where custid=".$custid." and (creditnotenumber = '' or creditnotenumber is null)";
$resupdate = mysql_query($sqlupdate);
$rows = mysql_affected_rows();
echo $rows;
echo $sqlupdate;
			
if(($res1) and ($res2) and ($resupdate) and ($rows > 0))  
{  
	//*** Commit Transaction ***//  
	mysql_query("COMMIT TRANSACTION");
}
else
{
	//*** Rollback Transaction ***//  
	mysql_query("ROLLBACK TRANSACTION");
	echo('sql error');
exit;
}
$qry1 = "SELECT * from transactionmast where creditnotenumber = '".$creditnoteno."'";
$qry = mysql_query($qry1);
$data = array();
while($rows = mysql_fetch_array($qry))
{
	$data[] = array("trnid" => $rows['trnid'],
					"creditnotenumber" => $rows['creditnotenumber']);
}
echo json_encode($data);
?>